//
//  CYQueryandPayVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/1/5.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYQueryandPayVC.h"
#import "ParamModel.h"
#import "YSMD5.h"
#import "CYPayViewController.h"
#import "CYMeHttpTool.h"
#import "CYCompanyUserTool.h"
#import "CYLoginHttpTool.h"
#import "CYJumpsTool.h"
#import "CYCompanyUserModel.h"
#import "CYJsonInfo.h"
#import "MJExtension.h"


@interface CYQueryandPayVC ()<GopayNewPlatformDelegate>
@property (weak, nonatomic) IBOutlet UIButton *querybtn;
@property (weak, nonatomic) IBOutlet UIButton *paybtn;

@end

@implementation CYQueryandPayVC


-(void)viewWillAppear:(BOOL)animated{
    
    [_querybtn addTarget:self action:@selector(queryclick) forControlEvents:UIControlEventTouchUpInside];
    [_paybtn addTarget:self action:@selector(paybtnclick) forControlEvents:UIControlEventTouchUpInside];
   }
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)initData {
    
    // 初始化请求平台
    self.gopayNewPlatform = [[GopayNewPlatform alloc] init];
    self.gopayNewPlatform.delegate = self;
    
    // 初始化一些数值
    _tfVersion = @"2.1";
    _tfCharset = @"1";
    _tfLanguage = @"1";
    _tfSignType = @"1";
    _tfTranCode = @"8888";
    _tfMerchantID = @"0000011536";
    _tfMerOrderNum = @"201601041411058686905";
    _tfTranAmt = @"0.01";
    _tfFeeAmt = @"0.00";
    _tfCurrencyType = @"156";
    _tfFrontMerUrl = @"";
    // _tfBackgroundMerUrl = @"https://mertest.gopay.com.cn/PGServer/Trans/WebClientAction.do";
    _tfBackgroundMerUrl = @"http://www.credit-manage.com:8058/phone/doPayResponse ";
    _tfTranDateTime = [ParamModel getSystemCurrentTime];
    _tfVirCardNoln = @"0000000002000004268";
    _tfTranIP = @"127.0.0.1";
    _tfIsRepeatSubmit = @"1";
    _tfGoodsName = @"天下无赖充值";
    _tfGoodsDetail = @"天下无赖充值";
    _tfBuyerName = @"MWAP";
    _tfBuyerContact = [GopayNewPlatform getGopayIdentifierForPayment];
    _tfMerRemark = @"";
    _tfmobileSighValue = [self setupMobileSingVaule];
    _tfSignValue = [self setupSingVaule];
}
- (NSString *)setupMobileSingVaule {
    
    NSMutableDictionary *MobileDic = [NSMutableDictionary dictionary];
    [MobileDic setValue:@"2.1" forKey:@"version"];
    [MobileDic setValue:@"8888" forKey:@"tranCode"];
    [MobileDic setValue:@"0000011536" forKey:@"merchantID"];
    [MobileDic setValue:[GopayNewPlatform getGopayIdentifierForPayment] forKey:@"buyerContact"];
    [MobileDic setValue:@"201601041411058686905" forKey:@"merOrderNum"];
    [MobileDic setValue:@"creditmanage" forKey:@"VerficationCode"];
    
    NSString *mobileSingVaule = [ParamModel mobileSignValueWithDictionary:MobileDic];
   // NSLog(@"加密后的mobileSingVaule:%@", mobileSingVaule);
    return mobileSingVaule;
}

- (NSString *)setupSingVaule {
    
    NSMutableDictionary *signDic = [NSMutableDictionary dictionary];
    [signDic setValue:@"2.1" forKey:@"version"];
    [signDic setValue:@"8888" forKey:@"tranCode"];
    [signDic setValue:@"0000011536" forKey:@"merchantID"];
    [signDic setValue:@"201601041411058686905" forKey:@"merOrderNum"];
    [signDic setValue:@"0.01" forKey:@"tranAmt"];
    [signDic setValue:@"0.00" forKey:@"feeAmt"];
    [signDic setValue:[ParamModel getSystemCurrentTime] forKey:@"tranDateTime"];
    [signDic setValue:@"" forKey:@"frontMerUrl"];
    //[signDic setValue:@"https://mertest.gopay.com.cn/PGServer/Trans/WebClientAction.do" forKey:@"backgroundMerUrl"];
    [signDic setValue:@"http://www.credit-manage.com:8058/phone/doPayResponse " forKey:@"backgroundMerUrl"];
    
    [signDic setValue:@"" forKey:@"orderId"];
    [signDic setValue:@"" forKey:@"gopayOutOrderId"];
    [signDic setValue:@"127.0.0.1" forKey:@"tranIP"];
    [signDic setValue:@"" forKey:@"respCode"];
    [signDic setValue:@"" forKey:@"gopayServerTime"];
    [signDic setValue:@"creditmanage" forKey:@"VerficationCode"];
    
    NSString *signValue = [ParamModel signValueWithDictionary:signDic];
   // NSLog(@"加密后的signValue:%@", signValue);
    
    return signValue;
}

- (NSDictionary *)setupParamModel {
    
    NSDictionary *param = [ParamModel createParamMoelWithVersion:_tfVersion
                                                         charset:_tfCharset
                                                        language:_tfLanguage
                                                        signType:_tfSignType
                                                        tranCode:_tfTranCode
                                                      merchantID:_tfMerchantID
                                                     merOrderNum:_tfMerOrderNum
                                                         tranAmt:_tfTranAmt
                                                          feeAmt:_tfFeeAmt
                                                    currencyType:_tfCurrencyType
                                                     frontMerUrl:_tfFrontMerUrl
                                                backgroundMerUrl:_tfBackgroundMerUrl
                                                    tranDateTime:_tfTranDateTime
                                                     virCardNoIn:_tfVirCardNoln
                                                          tranIP:_tfTranIP
                                                  isRepeatSubmit:_tfIsRepeatSubmit
                                                       goodsName:_tfGoodsName
                                                     goodsDetail:_tfGoodsDetail
                                                       buyerName:_tfBuyerName
                                                    buyerContact:_tfBuyerContact
                                                       merRemark:_tfMerRemark
                                                 mobileSighValue:_tfmobileSighValue
                                                       signValue:_tfSignValue];
    return param;
}


-(void)queryclick{
    CYPayViewController *payvc = [[CYPayViewController alloc] init];
    [self.navigationController pushViewController:payvc animated:YES];
    if (self.userType == CYUserTypePerson) {
    
    CYUser *user = [CYLoginHttpTool userData].user;
    NSString *userid = user.userId;
    NSString *usertoken = user.token;
            [CYHttpTool postWithURL:[CYURL shared].getUserRechargeOrder params:@{@"userId":userid,@"token":usertoken, @"account":@"100.00"} success:^(id json) {
                CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
                if (jsonInfo.isSuccess) {
                    NSDictionary *data = jsonInfo.data;
                    NSArray *arr = [data valueForKey:@""];
                    if (arr) {
                        if (arr) {
//                            for (NSDictionary *dic in arr) {
//                                NSString *str = dic[@""];
//                                NSLog(@"str = %@",str);
//                            }
                        }
                    }
                }
                
//                CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
//                if (jsonInfo.isSuccess) {
//                    NSDictionary *data = jsonInfo.data;
//                    NSArray *arr = [data valueForKey:@"pageItems"];
//                    if (arr) {
//                        for (NSDictionary *dic in arr) {
//                            CYMessageModel *temp = [[CYMessageModel alloc]initWithDictionary:dic];
//                            [_dataArray1 addObject:temp];
                
            } failure:^(NSError *error) {
//                NSLog(@"error = %@",error);
            }];
    }
    
    if (self.userType == CYUserTypeCompany) {
        //判断如果是公司的user
    CYCompanyUserModel *companyuser = [CYCompanyUserTool companyUser];
    NSString *carcompanyUserid = companyuser.carCompanyUserId;
    NSString *companytoken = companyuser.token;
//    NSLog(@"carcompanyUserid = %@,companytoken = %@",carcompanyUserid,companytoken);
    [CYHttpTool postWithURL:[CYURL shared].getCompanyRechargeOrder params:@{@"userId":carcompanyUserid,@"token":companytoken,@"account":@"100.00"} success:^(id json) {
        NSLog(@"jsoncom = %@",json);

    } failure:^(NSError *error) {
        
    }];
  }
}

-(void)paybtnclick{
   // NSLog(@"网络请求参数%@", [self setupParamModel]);
    [self.gopayNewPlatform startGopayWithParameters:[self setupParamModel]];
    
}

#pragma mark - GopayNewPlatformDelegate
-(void)gopayDidClosed{
    //do anything what you want
//    NSLog(@"已经关闭交易");
}

#pragma mark - 
+ (void)rechargeAmount:(CGFloat)amount {
    
}
@end
